#include "general.h"
#if (SENSOR_TYPE == Sensor_MI1320)
#include "pv_api.h"
#include "sensor_cdsp.h"
#include "sp1k_aaa_api.h"

UINT8 sensorDriverGainClass = 0xff;
UINT8	RawR[256],RawG[256],RawB[256];

UINT8 last_gain;
UINT32 last_shutter;

#define MAX_FRAME_RATE_30_IDX 127	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_30_IDX 0	// New limited for OV3630 - Jim@0424
#define MAX_FRAME_RATE_15_IDX 127	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_15_IDX 0	// New limited for OV3630 - Jim@0424

#define MAX_FRAME_RATE_25_IDX 127
#define MIN_FRAME_RATE_25_IDX 0
#define MAX_FRAME_RATE_12P5_IDX 127
#define MIN_FRAME_RATE_12P5_IDX 0

#define MAX_GAIN_IDX (175)
#define MIN_GAIN_IDX  (0)



code AEstr LVtbl50[128] = {
		{0,	 334,	 172,	 0	},
		{1,	 333,	 163,	 0	},
		{2,	 334,	 156,	 0	},
		{3,	 333,	 148,	 0	},
		{4,	 334,	 142,	 0	},
		{5,	 333,	 135,	 0	},
		{6,	 499,	 170,	 0	},
		{7,	 333,	 124,	 0	},
		{8,	 333,	 119,	 0	},
		{9,	 333,	 114,	 0	},
		{10,	 333,	 107,	 0	},
		{11,	 333,	 99,	 0	},
		{12,	 502,	 129,	 0	},
		{13,	 333,	 84,	 0	},
		{14,	 499,	 118,	 0	},
		{15,	 333,	 71,	 0	},
		{16,	 499,	 106,	 0	},
		{17,	 991,	 161,	 0	},
		{18,	 333,	 55,	 0	},
		{19,	 998,	 147,	 0	},
		{20,	 1005,	 139,	 0	},
		{21,	 1019,	 137,	 0	},
		{22,	 1005,	 129,	 0	},
		{23,	 998,	 123,	 0	},
		{24,	 1012,	 119,	 0	},
		{25,	 991,	 113,	 0	},
		{26,	 499,	 45,	 0	},
		{27,	 991,	 97,	 0	},
		{28,	 991,	 95,	 0	},
		{29,	 998,	 83,	 0	},
		{30,	 1005,	 72,	 0	},
		{31,	 1019,	 78,	 0	},
		{32,	 1005,	 65,	 0	},
		{33,	 998,	 57,	 0	},
		{34,	 1012,	 55,	 0	},
		{35,	 991,	 49,	 0	},
		{36,	 998,	 45,	 0	},
		{37,	 333,	 3,	 0	},
		{38,	 998,	 37,	 0	},
		{39,	 502,	 9,	 0	},
		{40,	 991,	 25,	 0	},
		{41,	 1019,	 28,	 0	},
		{42,	 991,	 24,	 0	},
		{43,	 1012,	 21,	 0	},
		{44,	 998,	 19,	 0	},
		{45,	 1005,	 17,	 0	},
		{46,	 1012,	 15,	 0	},
		{47,	 1019,	 13,	 0	},
		{48,	 1012,	 11,	 0	},
		{49,	 1005,	 9,	 0	},
		{50,	 991,	 7,	 0	},
		{51,	 1019,	 6,	 0	},
		{52,	 991,	 4,	 0	},
		{53,	 1012,	 3,	 0	},
		{54,	 1019,	 2,	 0	},
		{55,	 991,	 0,	 0	},
		{56,	 1178,	 2,	 0	},
		{57,	 1123,	 0,	 0	},
		{58,	 1579,	 5,	 0	},
		{59,	 1579,	 3,	 0	},
		{60,	 1382,	 0,	 0	},
		{61,	 2083,	 22,	 0	},
		{62,	 2083,	 18,	 0	},
		{63,	 2764,	 13,	 0	},
		{64,	 2764,	 10,	 0	},
		{65,	 2356,	 6,	 0	},
		{66,	 2356,	 2,	 0	},
		{67,	 2764,	 7,	 0	},
		{68,	 2764,	 5,	 0	},
		{69,	 2764,	 3,	 0	},
		{70,	 4956,	 12,	 0	},
		{71,	 4956,	 10,	 0	},
		{72,	 4956,	 9,	 0	},
		{73,	 5527,	 10,	 0	},
		{74,	 5527,	 8,	 0	},
		{75,	 5527,	 6,	 0	},
		{76,	 4491,	 2,	 0	},
		{77,	 4491,	 0,	 0	},
		{78,	 13065,	 28,	 0	},
		{79,	 13065,	 25,	 0	},
		{80,	 13065,	 22,	 0	},
		{81,	 13065,	 20,	 0	},
		{82,	 13065,	 17,	 0	},
		{83,	 11055,	 10,	 0	},
		{84,	 47903,	 86,	 0	},
		{85,	 47903,	 81,	 0	},
		{86,	 13065,	 9,	 0	},
		{87,	 11055,	 3,	 0	},
		{88,	 11055,	 2,	 0	},
		{89,	 11055,	 1,	 0	},
		{90,	 11055,	 0,	 0	},
		{91,	 20530,	 12,	 0	},
		{92,	 20530,	 10,	 0	},
		{93,	 20530,	 8,	 0	},
		{94,	 20530,	 6,	 0	},
		{95,	 23952,	 8,	 0	},
		{96,	 23952,	 7,	 0	},
		{97,	 17964,	 0,	 0	},
		{98,	 35927,	 14,	 0	},
		{99,	 35927,	 12,	 0	},
		{100,	 35927,	 10,	 0	},
		{101,	 143698,	 81,	 0	},
		{102,	 143698,	 75,	 0	},
		{103,	 143698,	 68,	 0	},
		{104,	 143698,	 63,	 0	},
		{105,	 143698,	 57,	 0	},
		{106,	 143698,	 50,	 0	},
		{107,	 143698,	 43,	 0	},
		{108,	 143698,	 28,	 0	},
		{109,	 143698,	 12,	 0	},
		{110,	 143698,	 10,	 0	},
		{111,	 143698,	 32,	 0	},
		{112,	 143698,	 29,	 0	},
		{113,	 143698,	 26,	 0	},
		{114,	 143698,	 23,	 0	},
		{115,	 143698,	 21,	 0	},
		{116,	 143698,	 18,	 0	},
		{117,	 143698,	 16,	 0	},
		{118,	 143698,	 14,	 0	},
		{119,	 143698,	 12,	 0	},
		{120,	 143698,	 10,	 0	},
		{121,	 143698,	 8,	 0	},
		{122,	 143698,	 7,	 0	},
		{123,	 143698,	 5,	 0	},
		{124,	 143698,	 4,	 0	},
		{125,	 143698,	 2,	 0	},
		{126,	 143698,	 1,	 0	},
		{127,	 143698,	 0,	 0	},
	};



code AEstr LVtbl60[130] = {
		{0,	 299,	 175,	 0	},
		{1,	 300,	 167,	 0	},
		{2,	 299,	 159,	 0	},
		{3,	 301,	 152,	 0	},
		{4,	 301,	 145,	 0	},
		{5,	 299,	 138,	 0	},
		{6,	 299,	 132,	 0	},
		{7,	 400,	 153,	 0	},
		{8,	 400,	 146,	 0	},
		{9,	 399,	 139,	 0	},
		{10,	 301,	 112,	 0	},
		{11,	 300,	 103,	 0	},
		{12,	 299,	 95,	 0	},
		{13,	 399,	 117,	 0	},
		{14,	 301,	 81,	 0	},
		{15,	 299,	 74,	 0	},
		{16,	 299,	 68,	 0	},
		{17,	 400,	 89,	 0	},
		{18,	 400,	 82,	 0	},
		{19,	 399,	 75,	 0	},
		{20,	 301,	 47,	 0	},
		{21,	 1218,	 170,	 0	},
		{22,	 399,	 58,	 0	},
		{23,	 399,	 53,	 0	},
		{24,	 601,	 81,	 0	},
		{25,	 1198,	 138,	 0	},
		{26,	 1198,	 132,	 0	},
		{27,	 1208,	 126,	 0	},
		{28,	 400,	 33,	 0	},
		{29,	 1208,	 117,	 0	},
		{30,	 301,	 15,	 0	},
		{31,	 399,	 22,	 0	},
		{32,	 399,	 20,	 0	},
		{33,	 301,	 8,	 0	},
		{34,	 1218,	 82,	 0	},
		{35,	 1198,	 74,	 0	},
		{36,	 1198,	 68,	 0	},
		{37,	 1208,	 63,	 0	},
		{38,	 1198,	 57,	 0	},
		{39,	 1208,	 53,	 0	},
		{40,	 1208,	 48,	 0	},
		{41,	 1208,	 44,	 0	},
		{42,	 1208,	 40,	 0	},
		{43,	 1208,	 36,	 0	},
		{44,	 1218,	 33,	 0	},
		{45,	 1198,	 29,	 0	},
		{46,	 1198,	 26,	 0	},
		{47,	 1218,	 24,	 0	},
		{48,	 1208,	 21,	 0	},
		{49,	 1198,	 18,	 0	},
		{50,	 1208,	 16,	 0	},
		{51,	 1208,	 14,	 0	},
		{52,	 1208,	 12,	 0	},
		{53,	 1208,	 10,	 0	},
		{54,	 1198,	 8,	 0	},
		{55,	 1218,	 7,	 0	},
		{56,	 1198,	 5,	 0	},
		{57,	 1218,	 4,	 0	},
		{58,	 1198,	 2,	 0	},
		{59,	 1198,	 1,	 0	},
		{60,	 1208,	 0,	 0	},
		{61,	 1796,	 16,	 0	},
		{62,	 1796,	 14,	 0	},
		{63,	 1796,	 12,	 0	},
		{64,	 2083,	 8,	 0	},
		{65,	 2764,	 12,	 0	},
		{66,	 2764,	 9,	 0	},
		{67,	 2356,	 3,	 0	},
		{68,	 2356,	 2,	 0	},
		{69,	 2246,	 1,	 0	},
		{70,	 2764,	 2,	 0	},
		{71,	 2764,	 1,	 0	},
		{72,	 4956,	 14,	 0	},
		{73,	 4956,	 11,	 0	},
		{74,	 4956,	 9,	 0	},
		{75,	 5527,	 10,	 0	},
		{76,	 5527,	 8,	 0	},
		{77,	 5527,	 6,	 0	},
		{78,	 5527,	 4,	 0	},
		{79,	 5527,	 2,	 0	},
		{80,	 15968,	 37,	 0	},
		{81,	 28742,	 73,	 0	},
		{82,	 28742,	 69,	 0	},
		{83,	 28742,	 63,	 0	},
		{84,	 13065,	 17,	 0	},
		{85,	 11055,	 9,	 0	},
		{86,	 47903,	 89,	 0	},
		{87,	 47903,	 82,	 0	},
		{88,	 13065,	 7,	 0	},
		{89,	 13065,	 6,	 0	},
		{90,	 13065,	 5,	 0	},
		{91,	 11055,	 3,	 0	},
		{92,	 11055,	 0,	 0	},
		{93,	 71853,	 81,	 0	},
		{94,	 20530,	 10,	 0	},
		{95,	 71853,	 65,	 0	},
		{96,	 71853,	 63,	 0	},
		{97,	 47903,	 33,	 0	},
		{98,	 23952,	 7,	 0	},
		{99,	 17964,	 0,	 0	},
		{100,	 143698,	 103,	 0	},
		{101,	 143698,	 95,	 0	},
		{102,	 143698,	 88,	 0	},
		{103,	 143698,	 81,	 0	},
		{104,	 143698,	 75,	 0	},
		{105,	 143698,	 68,	 0	},
		{106,	 143698,	 63,	 0	},
		{107,	 143698,	 59,	 0	},
		{108,	 143698,	 56,	 0	},
		{109,	 143698,	 50,	 0	},
		{110,	 143698,	 45,	 0	},
		{111,	 143698,	 40,	 0	},
		{112,	 143698,	 36,	 0	},
		{113,	 143698,	 32,	 0	},
		{114,	 143698,	 29,	 0	},
		{115,	 143698,	 26,	 0	},
		{116,	 143698,	 23,	 0	},
		{117,	 143698,	 21,	 0	},
		{118,	 143698,	 18,	 0	},
		{119,	 143698,	 16,	 0	},
		{120,	 143698,	 14,	 0	},
		{121,	 143698,	 12,	 0	},
		{122,	 143698,	 10,	 0	},
		{123,	 143698,	 8,	 0	},
		{124,	 143698,	 7,	 0	},
		{125,	 143698,	 5,	 0	},
		{126,	 143698,	 4,	 0	},
		{127,	 143698,	 2,	 0	},
		{128,	 143698,	 1,	 0	},
		{129,	 143698,	 0,	 0	},
	};



code UINT8 gaintbl[176] = {
	16, 	17, 	18, 	19, 	20, 	21, 	22, 	23, 	24, 	25,
	26, 	27, 	28, 	29, 	30, 	31, 	32, 	33, 	34, 	35,
	36, 	37, 	38, 	39, 	40, 	41, 	42, 	43, 	44, 	45,
	46, 	47, 	48, 	49, 	50, 	51, 	52, 	53, 	54, 	55,
	56, 	57, 	58, 	59, 	60, 	61, 	62, 	63, 	64, 	65,
	66, 	67, 	68, 	69, 	70, 	71, 	72, 	73, 	74, 	75,
	76, 	77, 	78, 	79, 	80, 	81, 	82, 	83, 	84, 	85,
	86, 	87, 	88, 	89, 	90, 	91, 	92, 	93, 	94, 	95,
	96, 	97, 	98, 	99, 	100, 	101, 	102, 	103, 	104, 	105,
	106, 	107, 	108, 	109, 	110, 	111, 	112, 	113, 	114, 	115,
	116, 	117, 	118, 	119, 	120, 	121, 	122, 	123, 	124, 	125,
	126, 	127, 	192, 	193, 	194, 	195, 	196, 	197, 	198, 	199,
	200, 	201, 	202, 	203, 	204, 	205, 	206, 	207, 	208, 	209,
	210, 	211, 	212, 	213, 	214, 	215, 	216, 	217, 	218, 	219,
	220, 	221, 	222, 	223, 	224, 	225, 	226, 	227, 	228, 	229,
	230, 	231, 	232, 	233, 	234, 	235, 	236, 	237, 	238, 	239,
	240, 	241, 	242, 	243, 	244, 	245, 	246, 	247, 	248, 	249,
	250, 	251, 	252, 	253, 	254, 	255,

};



#define K_GAIN_Large        45
#define K_GAIN_Middle		30
#define K_GAIN_Small		15

code UINT16 cmt[3][9] = {
	{0x0040,0x03ff,0x03ff,0x03ff,0x0040,0x03ff,0x03ff,0x03ff,0x0040},
	{0x0045,0x03EF,0x000C,0x03E8,0x006B,0x03EC,0x03F0,0x03F4,0x005C},
	{0x0029,0x0010,0x0006,0x03F4,0x0058,0x03F4,0x03F8,0x0015,0x0033}};

#define DAY_H_INDEX	26
#define DAY_L_INDEX	17
#define CWF_H_INDEX	17
#define CWF_L_INDEX	10
#define TUNG_H_INDEX	10
#define TUNG_L_INDEX	0
#define CLOUDY_H_INDEX	29
#define CLOUDY_L_INDEX	21

UINT8 gDAY_H_INDEX;
UINT8 gDAY_L_INDEX;
UINT8 gCWF_H_INDEX;
UINT8 gCWF_L_INDEX;
UINT8 gTUNG_H_INDEX;
UINT8 gTUNG_L_INDEX;
UINT8 gCLOUDY_H_INDEX;
UINT8 gCLOUDY_L_INDEX;

#define	AWBCTT_TBL_MAX	30

code UINT16 AWBCTT_TBL[AWBCTT_TBL_MAX][2] = {
{	206	,	422	},
{	218	,	419	},
{	229	,	416	},
{	239	,	413	},
{	248	,	411	},
{	257	,	408	},
{	265	,	406	},
{	273	,	404	},
{	284	,	401	},
{	294	,	399	},
{	303	,	396	},
{	311	,	394	},
{	321	,	391	},
{	330	,	389	},
{	335	,	381	},
{	340	,	374	},
{	345	,	367	},
{	349	,	360	},
{	355	,	352	},
{	360	,	345	},
{	364	,	338	},
{	369	,	332	},
{	374	,	324	},
{	379	,	316	},
{	384	,	309	},
{	388	,	302	},
{	393	,	295	},
{	398	,	288	},
{	402	,	281	},
{	407	,	275	}

};

#define SP_AWBCTT_TBL_MAX 1

code UINT16 SP_AWBCTT_TBL[SP_AWBCTT_TBL_MAX][3] = {
{383,295,2}
};

UINT8 gSP_AWBCTT_TBL_MAX;

UINT16 ctc_rgain[4]={230,250,288,0};
UINT16 ctc_bgain[4]={520,480,450,0};

#define MCLKMODE        0 /*mclk mode,'1'from I/O Pad,'0'from internal*/
#define PCLKMODE        1 /*pclk mode,'1'from I/O Pad,'0'from internal*/
#define PREV_MCLKDIV    2 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define PREV_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#define SNAP_MCLKDIV    2 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define SNAP_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#if (PCLKMODE == 1)
#define PREV_PCLKFREQ (UINT32) 12000000
#define SNAP_PCLKFREQ (UINT32) 24000000
#define CLK_RATIO     (UINT8) ((SNAP_PCLKFREQ/1000) * 128 / (PREV_PCLKFREQ/1000))
#else
#define PREV_PCLKFREQ (UINT32) 96000000 / PREV_MCLKDIV / PREV_PCLKDIV
#define SNAP_PCLKFREQ (UINT32) 96000000 / SNAP_MCLKDIV / SNAP_PCLKDIV
#define CLK_RATIO     (UINT8) ((UINT16) PREV_MCLKDIV * PREV_PCLKDIV * 128 / SNAP_MCLKDIV / SNAP_PCLKDIV)
#endif
#define PCLKphase 0  /*0~15*/
#define TGGPIOFUNC 0
#define TGGPIODIR 0x04
#define TGGPIOVAL 0

xdata UINT16 exptime;
extern xdata UINT32 G_SHUTTER;
extern xdata UINT8 G_GAIN;
extern xdata UINT8 PreGain;
extern xdata UINT32 PreShutter;
extern UINT8 HAL_CDSPeofWait( UINT8 Number);
extern void cdspColorMatrixRestore_Intr(void);
extern void cdspColorMatrixRestore(void);

void  HAL_MicronI2C_Write(UINT8 RegAddr, UINT8 RegLData, UINT8 RegHData)
{
	UINT8 reg_addr[1], reg_data[2];
	reg_addr[0] = RegAddr;
 	reg_data[0] = RegHData;
	reg_data[1] = RegLData;
 	HAL_WriteSSC(reg_addr, reg_data, 0x02, 0x00);  //Set Seq SSC Write
}

void  HAL_FrontInit (UINT8 CamMode) USING_0
{
	UINT8 tmp0;
	printf (" Initialize the Front Sensor_MI5100 : Enter !!,CamMode=%d \n",CamMode);

	HAL_SetFrontIOConfig(TGGPIOFUNC, 0x00, TGGPIODIR, TGGPIOVAL);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	tmp0 = XBYTE[0x20E4];
	tmp0 &= 0xF0;
	tmp0 |= PCLKphase;
	XBYTE[0x20E4] |= tmp0;
	//XBYTE[0x20E3] |= 0x08;
	HAL_SSCConfig(Sensor_Slave_Address, Sensor_SSC_Freq);
	XBYTE[0x2908] = 0x01;
	XBYTE[0x290c] = 0x01;
	XBYTE[0x2914] = 0x22;/**/
	XBYTE[0x2AB0] = 0x01;		      //Reset Sensor interface
	XBYTE[0x2AB0] = 0x00;
	HAL_SensorRegInit();
	gDAY_H_INDEX = DAY_H_INDEX;
	gDAY_L_INDEX = DAY_L_INDEX;
	gCWF_H_INDEX = CWF_H_INDEX;
	gCWF_L_INDEX = CWF_L_INDEX;
	gTUNG_H_INDEX = TUNG_H_INDEX;
	gTUNG_L_INDEX = TUNG_L_INDEX;
	gCLOUDY_H_INDEX = CLOUDY_H_INDEX;
	gCLOUDY_L_INDEX = CLOUDY_L_INDEX;
	gSP_AWBCTT_TBL_MAX = SP_AWBCTT_TBL_MAX;
}

void  HAL_FrontPreviewSet(void) USING_0
{
	HAL_FrontVdWait(0,1);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	HAL_SetFrontImgCrop(Sensor_Preview_HOffset, Sensor_Preview_VOffset, Sensor_Preview_HSize, Sensor_Preview_VSize, 0x00);
	HAL_SetFrontInverse(Prev_Hsync_Inverse,Prev_Vsync_Inverse);
	HAL_SetFrontReshape(Sensor_Preview_Hfall, Sensor_Preview_Hrise, Sensor_Preview_HReshen, Sensor_Preview_Vfall, Sensor_Preview_Vrise, Sensor_Preview_VReshen);
	HAL_SetSensorPreview();
	XBYTE[0x2110]=Sensor_Preview_BayerPattern;
}
void  HAL_FrontSnapSet(UINT16 Hsize,UINT16 Vsize) USING_0
{
	HAL_SetFrontCLKSet(SNAP_MCLKDIV, SNAP_PCLKDIV, 0);
	HAL_SetFrontReshape(Sensor_Snap_Hfall, Sensor_Snap_Hrise, Sensor_Snap_HReshen, Sensor_Snap_Vfall, Sensor_Snap_Vrise, Sensor_Snap_VReshen);
	HAL_SetFrontImgCrop(Sensor_Snap_HOffset, Sensor_Snap_VOffset, Hsize, Vsize, 1);
	HAL_SetFrontInverse(Snap_Hsync_Inverse,Snap_Vsync_Inverse);
	XBYTE[0x2110]=Sensor_Snap_BayerPattern;
	PreGain =0;
	PreShutter = 0;
}
void  HAL_SensorRegInit(void)
{
#if 0
	HAL_MicronI2C_Write(0x0D,0x03,0x00);
  	HAL_Wait(300);
	HAL_MicronI2C_Write(0x0D,0x08,0x00);
	HAL_MicronI2C_Write(0xF0,0x00,0x00);
	HAL_MicronI2C_Write(0xc8,0x00,0x00);
	HAL_MicronI2C_Write(0x21,0x0c,0x00);
	HAL_MicronI2C_Write(0x07,0xbe,0x00);
	HAL_MicronI2C_Write(0x08,0x1e,0x00);
	HAL_MicronI2C_Write(0xF0,0x01,0x00);
	HAL_MicronI2C_Write(0x06,0x00,0x80);
	HAL_MicronI2C_Write(0x3A,0x00,0x57);
	HAL_MicronI2C_Write(0x9B,0x00,0x57);
	HAL_MicronI2C_Write(0xF0,0x00,0x00);
#else
	HAL_MicronI2C_Write(0x0D,0x01,0x00);
  	HAL_Wait(300);
	HAL_MicronI2C_Write(0x0D,0x08,0x00);
	HAL_MicronI2C_Write(0xF0,0x00,0x00);
	HAL_MicronI2C_Write(0xc8,0x00,0x00);
	HAL_MicronI2C_Write(0x21,0x0c,0x04);
	HAL_MicronI2C_Write(0x07,0xbe,0x00);
	HAL_MicronI2C_Write(0x08,0x1e,0x00);
	HAL_MicronI2C_Write(0xF0,0x01,0x00);
	HAL_MicronI2C_Write(0x06,0x00,0x00);
	HAL_MicronI2C_Write(0x08,0x00,0x00);
	HAL_MicronI2C_Write(0x3A,0x00,0x57);
	HAL_MicronI2C_Write(0x9B,0x00,0x57);
	HAL_MicronI2C_Write(0xF0,0x00,0x00);
#endif


}


void HAL_SetSensorPreview(void) USING_0
{
#if 0
	HAL_MicronI2C_Write(0xF0,0x00,0x00);
	HAL_MicronI2C_Write(0xc8,0x00,0x00);
	HAL_MicronI2C_Write(0x21,0x0c,0x00);
	HAL_MicronI2C_Write(0x07,0xbe,0x00);
	HAL_MicronI2C_Write(0x08,0x1e,0x00);
	HAL_MicronI2C_Write(0x20,0x03,0x01);
#else
	HAL_MicronI2C_Write(0xF0,0x00,0x00);
	HAL_MicronI2C_Write(0xc8,0x00,0x00);
	HAL_MicronI2C_Write(0x21,0x0c,0x04);
	HAL_MicronI2C_Write(0x20,0x03,0x01);
	HAL_MicronI2C_Write(0x07,0xbe,0x00);
	HAL_MicronI2C_Write(0x08,0x1e,0x00);
	//HAL_MicronI2C_Write(0x0a,0x11,0x81);
#endif
}




/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontGainSet                                                     *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Sensor gain                                                           *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust gain value for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, gain value to set                                                    *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/
UINT8 HAL_FrontGainSet(
    UINT32 value,
    UINT32  option
)USING_1
{
	UINT32 tmp0;
	UINT8 regaddr[1], regdata[2];
	UINT8 tgain;

	tmp0 = option & 0x01;
	tgain= gaintbl[ value];

	regaddr[0] = 0x2f;
	regdata[0] = 0x00;
	regdata[1] = tgain;
	HAL_WriteSSC(regaddr, regdata, 0x02, 0);

	//printf("gain = %bu\n",last_gain);
	return SUCCESS;
}

UINT8 HAL_FrontGainSet_Intr(
    UINT32 value,
    UINT32  option
)USING_1
{

	UINT32 tmp0;
	UINT8 regaddr[1], regdata[2];
	UINT16 tgain;

	tmp0 = option & 0x01;
	tgain= gaintbl[ value];
	last_gain=tgain;

	regaddr[0] = 0x2f;
	regdata[0] = 0x00;
	regdata[1] = tgain;
	HAL_WriteSSC(regaddr, regdata, 0x02, 0);

	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontExpTimeSet                                                  *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control exposure time                                                         *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust exposure time value                                                       *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, expossure time value to set, unit 0.1ms                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/
UINT32
HAL_FrontExpTimeSet(
	UINT32 value,
	UINT32 option
) USING_1
{
	UINT16 ltotal, ftotal, espline;
	UINT32 pixclk;
	UINT8  regaddr[2], regdata[2];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */
	pixclk = PREV_PCLKFREQ;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;

	regaddr[0] = 0x09;
	regdata[0] = (espline & 0xff00) >> 8;
	regdata[1] = espline & 0xff;
	HAL_WriteSSC(regaddr, regdata, 0x02, option );
	return SUCCESS;
}

UINT32
HAL_FrontExpTimeSet_Intr(
	UINT32 value,
	UINT32 option
) USING_1
{

	UINT16 ltotal, ftotal, espline;
	UINT32 pixclk;
	UINT8  regaddr[2], regdata[2];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */
	pixclk = PREV_PCLKFREQ;

	last_shutter=value;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;

	regaddr[0] = 0x09;
	regdata[0] = (espline & 0xff00) >> 8;
	regdata[1] = espline & 0xff;
	HAL_WriteSSC_Intr(regaddr, regdata, 0x02, option );

	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontFrameRateSet                                                *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Frame Rate                                                            *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust Frame rate for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, frame rate value to set                                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT32
HAL_FrontFrameRateSet(
    UINT32 value,
    UINT32 option
)
{
#if 0
value = value;
	option = option;

	UINT32 ltotal, ftotal;
	UINT32 pixclk, tmp0;

	UINT8 regaddr[4], regdata[4];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */

	pixclk = PREV_PCLKFREQ;
	option = option & 0x01;

	tmp0=(pixclk * 10) /(ltotal * value);

	if (tmp0 >= ftotal ) {
		tmp0 =tmp0-ftotal;
	}
	else {
	    tmp0 =0;
	};
	regaddr[0] = 0x46;//0x2D;
	regdata[0] = tmp0;
	regaddr[1] = 0x47;//0x2E;
	regdata[1] = tmp0 >> 8;
	HAL_WriteSSC(regaddr, regdata, 0x02, option | 0x02);
#endif
	return 0;
}

void HAL_ExpTransToSnap(void)
{
	UINT16 ltotal, ftotal,espline;
	UINT32 tmp0,pixclk;
	UINT8  regaddr[7], regdata[7];
	UINT8  s_gain,lastt_gain;

	ltotal = Sensor_Snap_LineTotal;						/* Read line total */
	ftotal = Sensor_Snap_FrameTotal;					/* Read frame total */
	pixclk = SNAP_PCLKFREQ;

	espline = (pixclk * 10) / (ltotal * last_shutter);

	s_gain = last_gain;


	{
		lastt_gain=s_gain;
		espline=espline;
	}

	if (espline >= ftotal ) {
		tmp0 =espline-ftotal;
		espline = ftotal-1;
	}
	else {
		tmp0 =0;
	}
	regaddr[0] = 0x09;
	regdata[0] = (espline >> 8)&0xff;
	regdata[1] = (espline )&0xff;
	HAL_WriteSSC(regaddr, regdata, 0x02,  0x00);
	regaddr[0] = 0x35;
	regdata[0] = (lastt_gain>> 8)&0xff;
	regdata[1] = (lastt_gain)&0xff;
	HAL_WriteSSC(regaddr, regdata, 0x02,  0x00);
}

UINT8 HAL_SnapRaw8(UINT8 mode, UINT8 opt) USING_0
{
	mode = mode;
	if( opt == 1 )
	{
		HAL_FrontVvalidWait(1,1);
		HAL_MicronI2C_Write(0xf0,0x00,0x00);
		HAL_MicronI2C_Write(0xc8,0x0b,0x00);
		HAL_MicronI2C_Write(0x05,0x18,0x01);
		HAL_MicronI2C_Write(0x06,0x0d,0x00);
		HAL_MicronI2C_Write(0x20,0x03,0x01);

		//while(1)
		//	;
		//HAL_ExpTransToSnap();
	}

	HAL_FrontVvalidWait(0,1); // 20081024 mantis 32544 wenguo
	HAL_CdspWriteSkip( 6, 1 );
	HAL_FrontVvalidWait(0,1);
	HAL_FrontVvalidWait(1,1);
	HAL_FrontInputGate(0x11);
	HAL_CDSPeofWait(1);
	return 0;
}


void HalSensorPrePV(
	void
)
{
	HAL_FrontVvalidWait(0,1);
	HAL_FrontVvalidWait(0,1);
	return;
}

#if 1 /*cs added for gain*/
UINT8 HAL_MaxGainIndexGet()
{
	return(MAX_GAIN_IDX);
}
UINT8 HAL_MinGainIndexGet()
{
	return(MIN_GAIN_IDX);
}

UINT8 HAL_MaxExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MAX_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MAX_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MAX_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MAX_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

UINT8 HAL_MinExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MIN_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MIN_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MIN_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MIN_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

void HAL_ColorSatReduce_Intr(UINT8 idx)	   // reduce color saturation and edge
{
#if 0

	if(idx > K_GAIN_Large) {
		if( sensorDriverGainClass != 3 ) {
			HAL_CdspColorMSet_Intr(cmt[2][0],cmt[2][1],cmt[2][2],cmt[2][3],cmt[2][4],cmt[2][5],cmt[2][6],cmt[2][7],cmt[2][8]);
			XBYTE[0x21D3] = 0x08;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 3;
		}
	}
	else if (idx > K_GAIN_Middle) {
		if( sensorDriverGainClass != 2 ) {
			HAL_CdspColorMSet_Intr(cmt[1][0],cmt[1][1],cmt[1][2],cmt[1][3],cmt[1][4],cmt[1][5],cmt[1][6],cmt[1][7],cmt[1][8]);
			XBYTE[0x21D3] = 0x06;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 2;
		}
	}
	else if (idx > K_GAIN_Small) {
		if( sensorDriverGainClass != 1 ) {
			HAL_CdspColorMSet_Intr(cmt[0][0],cmt[0][1],cmt[0][2],cmt[0][3],cmt[0][4],cmt[0][5],cmt[0][6],cmt[0][7],cmt[0][8]); // for hgnoise
			XBYTE[0x21D3] = 0x04;
			XBYTE[0x21D0] = 0x0A;

			sensorDriverGainClass = 1;
		}
	}
	else {
		if( sensorDriverGainClass != 0 ) {
			cdspColorMatrixRestore_Intr();
			XBYTE[0x21D3] = 0x02;
			XBYTE[0x21D0] = 0x02;

			sensorDriverGainClass = 0;
		}
	};
#endif
}
#endif
#endif
